package com.snopy.controller;

import com.snopy.common.JSONResult;
import com.snopy.enums.CategoryEnum;
import com.snopy.enums.YesOrNoEnum;
import com.snopy.pojo.Carousel;
import com.snopy.pojo.Category;
import com.snopy.pojo.vo.CategoryVO;
import com.snopy.pojo.vo.NewItemsVO;
import com.snopy.service.CarouselService;
import com.snopy.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * TODO
 *
 * @author snoopy
 * @date 2020/9/20 0020 11:01
 * @email 77912204@qq.com
 * @remark
 */
@Api(value = "首页",tags = "首页展示的相关接口")
@RestController
@RequestMapping("index")
public class IndexController {



    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "获取首页轮播图列表",notes = "获取首页轮播图列表",httpMethod = "GET")
    @GetMapping("/carousel")
    public JSONResult carousel(){
        List<Carousel> carousels = carouselService.queryAll(YesOrNoEnum.YES.type);
        return JSONResult.ok(carousels);
    }

    @ApiOperation(value = "获取商品分类(一级分类)", notes = "获取商品分类(一级分类)", httpMethod = "GET")
    @GetMapping("/cats")
    public JSONResult cats(){
        List<Category> categories = categoryService.queryAllRootLevelCat(CategoryEnum.FIRST.type);
        return JSONResult.ok(categories);
    }

    @ApiOperation(value = "获取商品子分类", notes = "获取商品子分类", httpMethod = "GET")
    @GetMapping("//subCat/{rootCatId}")
    public JSONResult subcat(
            @ApiParam(name = "rootCatId", value = "一级分类id", required = true)
            @PathVariable Integer rootCatId){
        if (rootCatId==null){
            return JSONResult.errorMsg("分类不存在！");
        }
        List<CategoryVO> categories = categoryService.getSubCatList(rootCatId);
        return JSONResult.ok(categories);
    }


    @ApiOperation(value = "查询每个一级分类下的最新6条商品数据", notes = "查询每个一级分类下的最新6条商品数据", httpMethod = "GET")
    @GetMapping("/sixNewItems/{rootCatId}")
    public JSONResult sixNewItems(
            @ApiParam(name = "rootCatId", value = "一级分类id", required = true)
            @PathVariable Integer rootCatId) {

        if (rootCatId == null) {
            return JSONResult.errorMsg("分类不存在");
        }

        List<NewItemsVO> list = categoryService.getSixNewItemsLazy(rootCatId);
        return JSONResult.ok(list);
    }

}
